package com.itheima.controller;

import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Permission;
import com.itheima.service.PermissionService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.net.URLDecoder;
import java.util.Map;

/**
 * @Program: health_parent
 * @Description:
 * @Author: choulan
 * @Date:2020-11-24 10:44
 **/
@RestController
@RequestMapping("/permission")
public class PermissionController {

    @Reference
    private PermissionService permissionService;

    @PreAuthorize("hasAuthority('PERMISSION_QUERY')")
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        return permissionService.findPage(queryPageBean);
    }
    //新增权限
    @PreAuthorize("hasAuthority('PERMISSION_ADD')")
    @RequestMapping("/add")
    public Result add(@RequestBody Permission permission){
        try {
            permissionService.add(permission);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_PERMISSION_FALL);
        }
        return new Result(true,MessageConstant.ADD_PERMISSION_SUCCESS);
    }
    //根据权限id查询对应的权限信息
    @PreAuthorize("hasAuthority('PERMISSION_QUERY')")
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            Permission permission= permissionService.findById(id);
            return new Result(true,MessageConstant.ADD_PERMISSION_SUCCESS,permission);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_PERMISSION_FAIL);
        }
    }
    //根据权限id查询对应的权限信息
    @PreAuthorize("hasAuthority('PERMISSION_EDIT')")
    @RequestMapping("/edit")
    public Result edit(@RequestBody Permission permission){
        try {
             permissionService.edit(permission);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_PERMISSION_FAIL);
        }
        return new Result(true,MessageConstant.EDIT_PERMISSION_SUCCESS);
    }

    @RequestMapping("/checkName")
    public Result checkName(@RequestBody Map jsonData){
        try {
            String name = (String)jsonData.get("name");
            boolean isExsist= permissionService.checkName(name);
            return new Result(true,"校验成功",isExsist);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"校验失败");
        }
    }

    //根据权限id查询对应的权限信息
    @PreAuthorize("hasAuthority('PERMISSION_DELETE')")
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            permissionService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_PERMISSION_FAIL);
        }
        return new Result(true,MessageConstant.DELETE_PERMISSION_SUCCESS);
    }
}
